package dateStructure;

import java.util.Scanner;

public class circularQueue {
    //循环队列
    private static final int Maxsize=107;
    private static class MQueue{
        int base[];
        int front;
        int rear;
    }
    private static boolean initQueue(MQueue Q){
        Q.base=new int[Maxsize];
        if(Q.base==null){
            return false;
        }
        Q.front=Q.rear=0;
        return true;
    }
    private static boolean InQueue(MQueue Q,int e){
        if((Q.rear+1)%Maxsize==Q.front){
            System.out.println("队列已满！");
            return false;
        }
        Q.base[Q.rear]=e;
        Q.rear=(Q.rear+1)%Maxsize;
        return true;
    }
    private static int OutQueue(MQueue Q){
        if(Q.front==Q.rear){
            System.out.println("队列已空！");
            System.exit(0);
        }
        int x=Q.base[Q.front];
        Q.front=(Q.front+1)%Maxsize;
        return x;
    }
    public static int getElem(MQueue Q){
        if(Q.front==Q.rear){
            System.out.println("队列已空！");
            System.exit(0);
        }
        return Q.base[Q.front];
    }
    public static int getLength(MQueue Q){
        return (Q.rear-Q.front+Maxsize)%Maxsize;
    }
    public static void main(String arg[]){
        MQueue MyQ=new MQueue();
        System.out.println("该队列总容量为："+Maxsize);
        int x=-1;
        if(initQueue(MyQ)){
            System.out.println("初始化成功!");
        }
        Scanner scanner =new Scanner(System.in);
        System.out.print("请输入元素的个数：");
        int n=scanner.nextInt();
        int sum=n;
        System.out.print("请依次输入各个元素：");
        while (n-->0){
            x=scanner.nextInt();
            InQueue(MyQ,x);
        }
        System.out.println("队头元素为："+getElem(MyQ));
        System.out.println("队列的长度为："+getLength(MyQ));
        System.out.println("出队元素为：");
        while (sum-->0){
            System.out.print(OutQueue(MyQ)+" ");
        }
        System.out.println();
    }
}

